﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;

namespace Fitment.User
{
    public partial class myHouse : System.Web.UI.Page
    {
        DS data = new DS();
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                Session["House_ID"] = null;
                bindList();
            }
        }

        //protected void gv_myhouse_PageIndexChanging(object sender, GridViewPageEventArgs e)
        //{
        //    string select = gv_myhouse.SelectedRow.Cells[0].Text.ToString();//所选行的第二列的内容,即账号
        //    Session["House_ID"] = Convert.ToInt32(select);
        //}


        protected void newHouse_Click(object sender, EventArgs e)
        {
            Response.Redirect("houseInfo.aspx");
        }

        protected void editHouse_Click(object sender, EventArgs e)
        {
            if (Session["House_ID"] == null)
            {
                Page.ClientScript.RegisterClientScriptBlock(Page.GetType(), "提示", "<script>alert('请先选择您的房子！');</script>");
                return;
            }
            Session["flag"] = null;
            Response.Redirect("roomMatInfo.aspx");
        }
        //数据绑定
        protected void bindList()
        {
            string sql = "SELECT HOUSE_ID, ROOMCOUNT, DOORCOUNT, WINDCOUNT FROM HOUSEINFO WHERE USER_ID = '" + Convert.ToInt32(Session["User_ID"].ToString()) + "'";
            DataTable table = data.GetDataSource(sql, "Info");
            this.gv_myhouse.DataSource = table;
            this.gv_myhouse.DataBind();
        }

        protected void gv_myhouse_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            this.gv_myhouse.PageIndex = e.NewPageIndex;
            bindList();
        }

        protected void gv_myhouse_SelectedIndexChanging(object sender, GridViewSelectEventArgs e)
        {
            int index = e.NewSelectedIndex;
            Session["House_ID"] = gv_myhouse.DataKeys[index].Value;
        }

        protected void delete_Click(object sender, EventArgs e)
        {
            int houseId = Convert.ToInt32(Session["House_ID"].ToString());
            string sql = "DELETE FROM HOUSEINFO WHERE HOUSE_ID = '" + houseId + "'";
            data.ExcuteSql(sql);
            bindList();
        }
    }
}
